<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="{$core['describe']}">
    <meta name="author" content="Webpixels">
    <title>{$core['title']}</title>
    <!-- Favicon -->
    <link rel="icon" href="__PUBLIC__/../favicon.ico" type="image/png">
    <!-- Font Awesome 5 -->
    <link rel="stylesheet" href="//cdn.bootcdn.net/ajax/libs/font-awesome/5.15.3/css/all.min.css">
    <!-- Purpose CSS -->
    <link rel="stylesheet" href="__PUBLIC__/uikit/css/purpose.css" id="stylesheet">

    <link href="__PUBLIC__/Xadmin/lib/layui/css/modules/layer/default/layer.css" rel="stylesheet">
    <script type="text/javascript" src="//lib.baomitu.com/jquery/3.6.0/jquery.min.js"></script>
    <style>
        .layui-layer-btn0 {
            color: #ffffff !important;
        }
    </style>
</head>
<body class="application application-offset sidenav-pinned">
<!-- 应用程序容器 -->
<div class="container-fluid">
    <div class="page-content">
        <!-- 主体 -->
        <div class="row">
            <div class="col-xl-12 col-md-12 col-lg-12">
                <div class="card card-fluid">
                    <div class="card-body">
                        {if condition="$mode == 'add'"}

                        <form id="add_data">
                            <input type="hidden" name="type" value="{$type}">
                            <div class="form-group mb-1">
                                <label class="form-control-label">接口名称</label>
                                <input type="text" class="form-control" name="name" placeholder="请设置接口名称，便于查询" value="">
                            </div>
                            <div class="form-group">
                                <label class="form-control-label">支付插件</label>
                                <select class="form-control" name="plug_in">
                                    <option value="">请选择支付插件</option>
                                    {foreach name="$pay_extend" item="v" key="key"}
                                    <option value="{$key}">{$v.name}</option>
                                    {/foreach}
                                </select>
                            </div>
                            <div class="form-group">
                                <label class="form-control-label">金额限制</label>
                                <div class="input-group mb-2">
                                    <div class="input-group-prepend">
                                        <span class="input-group-text">至少支付</span>
                                    </div>
                                    <input type="text" class="form-control" name="min_money" placeholder="设置每次至少支付的金额"
                                           value="0.01">
                                    <div class="input-group-append">
                                        <span class="input-group-text">元</span>
                                    </div>
                                </div>
                                <div class="input-group mb-2">
                                    <div class="input-group-prepend">
                                        <span class="input-group-text">最多支付</span>
                                    </div>
                                    <input type="text" class="form-control" name="max_money" placeholder="设置每次最多支付的金额"
                                           value="9999999.99">
                                    <div class="input-group-append">
                                        <span class="input-group-text">元</span>
                                    </div>
                                </div>
                            </div>
                            <br>
                            <button type="button" class="btn btn-sm btn-primary rounded-pill" id="add_submit">确认添加
                            </button>
                        </form>

                        {elseif condition="$mode == 'edit'"}
                        <form id="edit_data">
                            <input type="hidden" name="id" value="{$pay['id']}">
                            <div class="form-group mb-1">
                                <label class="form-control-label">接口名称</label>
                                <input type="text" class="form-control" name="name" placeholder="请设置接口名称，便于查询"
                                       value="{$pay['name']}">
                            </div>
                            <div class="form-group">
                                <label class="form-control-label">支付插件</label>
                                <select class="form-control" name="plug_in">
                                    <option value="">请选择支付插件</option>
                                    {foreach name="$pay_extend" item="v" key="key"}
                                    <option value="{$key}" {$key==$pay['plug_in']?"selected":""}>{$v.name}</option>
                                    {/foreach}
                                </select>
                            </div>
                            <div class="form-group">
                                <label class="form-control-label">金额限制</label>
                                <div class="input-group mb-2">
                                    <div class="input-group-prepend">
                                        <span class="input-group-text">至少支付</span>
                                    </div>
                                    <input type="text" class="form-control" name="min_money" placeholder="设置每次至少支付的金额"
                                           value="{$pay['min_money']}">
                                    <div class="input-group-append">
                                        <span class="input-group-text">元</span>
                                    </div>
                                </div>
                                <div class="input-group mb-2">
                                    <div class="input-group-prepend">
                                        <span class="input-group-text">最多支付</span>
                                    </div>
                                    <input type="text" class="form-control" name="max_money" placeholder="设置每次最多支付的金额"
                                           value="{$pay['max_money']}">
                                    <div class="input-group-append">
                                        <span class="input-group-text">元</span>
                                    </div>
                                </div>
                            </div>
                            <br>
                            <button type="button" class="btn btn-sm btn-primary rounded-pill" id="edit_submit">确认修改
                            </button>
                        </form>
                        {elseif condition="$mode == 'con'"}
                        <script>
                            $(function () {
                                var pay_extend_val;
                                var Load = layer.load(2, {shade: [0.3, '#000']}); //开始加载
                                $.ajax({
                                    type: "POST",
                                    url: "/user/pay_inter/get_conn",
                                    data: {"pid":"{$pay['id']}"},
                                    dataType: "json",
                                    success: function (json) { //SUCCESS
                                        var data = $.parseJSON(json);
                                        if (data.code == 0) {
                                            layer.close(Load); //关闭加载
                                            pay_extend_val = data;
                                            var extend_data = $.parseJSON('{$pay_extend_json}');
                                            payExtendFormPlace("#con_form", extend_data, pay_extend_val);
                                        } else {
                                            layer.close(Load); //关闭加载
                                            var indexa = layer.alert(data.msg, {title: data.title, icon: 5}, function () {
                                                layer.close(indexa);
                                                //关闭iframe
                                                parent.layer.close(index);
                                            })
                                        }
                                    },
                                    error: function () { //ERROR
                                        layer.close(Load); //关闭加载
                                        layer.msg('系统繁忙');
                                        return false;
                                    }
                                });
                            })
                        </script>

                        <form id="con_data">
                            <input type="hidden" name="id" value="{$pay['id']}">
                            <div class="alert badge-success">
                                <b>插件名称：</b>{$pay_extend[$plug_in]['name']}<br>
                                <b>插件介绍：</b>{$pay_extend[$plug_in]['remarks']}
                            </div>
                            <div id="con_form"></div>
                            <button type="button" class="btn btn-sm btn-primary rounded-pill" id="con_submit">修改配置
                            </button>
                        </form>

                        {else}
                        <h1>
                            <center>不存在该操作类型</center>
                        </h1>
                        {/if}

                    </div>
                </div>
            </div>

        </div>
    </div>
</div>


<!-- Scripts -->
<script src="__PUBLIC__/uikit/js/purpose.js"></script>
<script src="__PUBLIC__/Xadmin/lib/layui/lay/modules/layer.js"></script>
<script>
    function payExtendFormPlace(e = null, arr = null, pay_extend_val) {
        if (arr == null || e == null || arr.length == 0) {
            return;
        }
        var html = "";
        for (var i = 1; i < 11; i++) {
            if (arr.form['value' + i + '_name'] != null && arr.form['value' + i + '_name'] != "") {
                if (arr.form['value' + i + '_data'] != null && ['value' + i + '_data'] != "") {
                    var data = arr.form['value' + i + '_data'];
                } else {
                    var data = null;
                }
                html += payExtendFormType(arr.form['value' + i + '_name'], arr.form['value' + i + '_type'], data, "value" + i, pay_extend_val['value' + i], arr.form['value' + i + '_rows']);
            }
        }


        $(con_form).html(html);
    }

    function payExtendFormType(name = null, type = null, data = null, m = null, val = null, rows = null) {
        if (rows == null) {
            rows = 1;
        }
        var html = "";
        html += '<div class="form-group"><label class="form-control-label">' + name + '</label>';
        if(val=="" || val==null){
            val = "";
        }
        switch (type) {
            case "text":
                html += ' <div class="input-group mb-2">';
                html += '<textarea type="text" class="form-control" rows="' + rows + '" name="' + m + '" placeholder="' + name + '">' + val + '</textarea>';
                break;
            case "wx_file_txt":
                html += ' <div class="input-group mb-2">';
                html += '<input type="text" class="form-control" name="' + m + '" placeholder="' + name + '" value="' + val + '">';
                break;
            case "radio":
                html += '<div class="radio">';
                for (var i = 0; i < data.length; i++) {
                    html += '<label><input type="radio" ' + (val == data[i].value ? "checked" : "") + ' name="' + m + '" value="' + data[i].value + '">&nbsp;' + data[i].title + '</label>&nbsp;&nbsp;';
                }
                html += "</div>";
                break
            case "checkbox":
                html += '<div className="checkbox">';
                if(val=="" || val==null){
                    var arr = [];
                }else{
                    var arr = val.split('|');
                }
                for (var i = 0; i < data.length; i++) {
                    html += '<label><input type="checkbox" ' + (arr.indexOf(data[i].value)!=-1 ? "checked" : "") + ' name="' + m + '[]" value="' + data[i].value + '">&nbsp;' + data[i].title + '</label>&nbsp;&nbsp';
                    }
                html += "</div>";
                break;
            default:
                return "";
        }
        html += "</div>";
        return html;
    }

    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
    $("#add_submit").click(function () {
        var Load = layer.load(2, {shade: [0.3, '#000']}); //开始加载
        $.ajax({
            type: "POST",
            url: "/user/pay_inter/add",
            data: $('#add_data').serialize(),
            dataType: "json",
            success: function (json) { //SUCCESS
                var data = $.parseJSON(json);
                if (data.code == 0) {
                    layer.close(Load); //关闭加载
                    var indexa = layer.msg(data.msg, {time: 1000, icon: 6}, function () {
                        layer.close(indexa);
                        //关闭iframe
                        parent.layer.close(index);
                    });
                } else {
                    layer.close(Load); //关闭加载
                    var indexa = layer.alert(data.msg, {title: data.title, icon: 5}, function () {
                        layer.close(indexa);
                        if (data.url != "" && data.url != null) {
                            window.location.href = data.url;
                        }
                    })
                }
            },
            error: function () { //ERROR
                layer.close(Load); //关闭加载
                layer.msg('系统繁忙');
                return false;
            }
        });
    });
    $("#con_submit").click(function () {
        if({$is_admin}==1) {
            layer.msg("管理员身份，安全密码可任意输入",{zIndex:99999999999});
        }
        var index1 = layer.prompt(
            {
                title: '请输入（6位）安全密码'
                , formType: 1
                , maxlength: 6
            },
            function (aqmm) {
                layer.close(index1);
                var Load = layer.load(2, {shade: [0.3, '#000']}); //开始加载
                $.ajax({
                    type: "POST",
                    url: "/user/pay_inter/con",
                    data: $('#con_data').serialize()+"&security_password="+aqmm,
                    dataType: "json",
                    success: function (json) { //SUCCESS
                        var data = $.parseJSON(json);
                        if (data.code == 0) {
                            layer.close(Load); //关闭加载
                            var indexa = layer.msg(data.msg, {time: 1000, icon: 6}, function () {
                                layer.close(indexa);
                                //关闭iframe
                                parent.layer.close(index);
                            });
                        } else {
                            layer.close(Load); //关闭加载
                            var indexa = layer.alert(data.msg, {title: data.title, icon: 5}, function () {
                                layer.close(indexa);
                                if (data.url != "" && data.url != null) {
                                    window.location.href = data.url;
                                }
                            })
                        }
                    },
                    error: function () { //ERROR
                        layer.close(Load); //关闭加载
                        layer.msg('系统繁忙');
                        return false;
                    }
                });
            });
    });
    $("#edit_submit").click(function () {
        var Load = layer.load(2, {shade: [0.3, '#000']}); //开始加载
        $.ajax({
            type: "POST",
            url: "/user/pay_inter/edit",
            data: $('#edit_data').serialize(),
            dataType: "json",
            success: function (json) { //SUCCESS
                var data = $.parseJSON(json);
                if (data.code == 0) {
                    layer.close(Load); //关闭加载
                    var indexa = layer.msg(data.msg, {time: 1000, icon: 6}, function () {
                        layer.close(indexa);
                        //关闭iframe
                        parent.layer.close(index);
                    });
                } else {
                    layer.close(Load); //关闭加载
                    var indexa = layer.alert(data.msg, {title: data.title, icon: 5}, function () {
                        layer.close(indexa);
                        if (data.url != "" && data.url != null) {
                            window.location.href = data.url;
                        }
                    })
                }
            },
            error: function () { //ERROR
                layer.close(Load); //关闭加载
                layer.msg('系统繁忙');
                return false;
            }
        });
    });
</script>
</body>
</html>